{% assign title = include.title | default: "Documents" %}
{% assign group = include.group | default: "@documents" %}
{% assign group_link = include.link %}
{% assign group_id = title | slugify %}
{% assign cover_width = include.width | default: "200" %}
{% assign cover_height = include.height | default: "" %}
{% assign path = include.path | default: include.target | default: page.permalink %}
{% assign path_parts = path | split: "/" %}
{% assign doc_style = include.style | default: "center" %}
{% assign doc_tag = include.tag | default: "" %}
{%- assign docs_exist = false -%}
{%- assign cur_path = "" -%}{%- assign parent = "" %}{%- assign category = "" -%}
{%- for part in path_parts -%}
  {%- if part == "" -%}{% continue %}{%- endif -%}
  {%- capture cur_path -%}{{ cur_path }}/{{ part }}{%- endcapture -%}
  {%- if site.developer -%}
    <!-- documents.html(0): category="{{ category }}", server="{{ server }}", folder="{{ part }}" -->
  {%- endif -%}
  {%- if part == "hardware" or part == "machines" -%}
    {%- assign category = "hardware" -%}
    {%- assign data = site.data.hardware -%}
  {%- elsif part == "software" -%}
    {%- assign category = part -%}
    {%- assign data = site.data.software -%}
  {%- elsif part == "documents" -%}
    {%- assign category = part -%}
    {%- assign data = site.data.documents -%}
  {%- else -%}
    {%- if part == "magazines" -%}{%- unless include.title -%}{%- assign title = "Issues" -%}{%- endunless -%}{%- endif -%}
    {%- if part == "*" -%}
      {%- assign cur_path = cur_path | replace: "/*", "" -%}
      {%- assign data = data['@versions'] -%}
      {%- assign docs = "" | split: "," -%}
      {%- for version in data -%}
        {%- for item in version[1][group] -%}
          {% assign docs = docs | push: item %}
        {%- endfor -%}
      {%- endfor -%}
    {%- else -%}
      {%- if site.developer -%}
        <!-- documents.html(1): checking folder='{{ part }}' -->
      {%- endif -%}
      {%- if data[part] -%}
        {%- if site.developer -%}
          <!-- documents.html(2): data['{{ part }}'] exists -->
        {%- endif -%}
        {%- assign parent = part %}
        {%- assign data = data[part] %}
        {%- if data['@server'] %}
          {%- assign server = data['@server'] | replace: "/disks/", "" %}
        {%- endif %}
        {%- if data['']['@type'] -%}
          {%- assign type = data['']['@type'] -%}
          {%- assign data = site.data[part][type] -%}
        {%- elsif data[part]['@type'] -%}
          {%- assign type = data[part]['@type'] -%}
          {%- assign data = site.data[part][type] -%}
        {%- endif -%}
      {%- elsif data['@products'] -%}
        {%- if site.developer -%}
          <!-- documents.html(3): data['@products'] exists -->
        {%- endif -%}
        {%- assign data = data['@products'][part] -%}
      {%- elsif data['@versions'] -%}
        {%- if site.developer -%}
          <!-- documents.html(4): data['@versions'] exists -->
        {%- endif -%}
        {%- assign tmp = data['@versions'][part] -%}
        {%- if tmp -%}
          {%- assign data = tmp -%}
        {%- else -%}
          {%- assign i = forloop.index -%}
          {%- capture tmp -%}{{ part }}/{{ path_parts[i] }}{%- endcapture -%}
          {%- capture cur_path -%}{{ cur_path }}/{{ path_parts[i] }}{%- endcapture -%}
          {%- assign data = data['@versions'][tmp] -%}
        {%- endif -%}
      {%- else %}
        {%- assign data = data['@instances'][part] -%}
        {%- if site.developer -%}
          <!-- documents.html(5): data = data['@instances']['{{ part }}'] -->
        {%- endif -%}
        {%- if data['@type'] -%}
          {%- assign type = data['@type'] -%}
          {%- assign data = site.data[part][type] -%}
          {%- if data %}
            {%- if site.developer -%}
              <!-- documents.html(6): data = site.data['{{ part }}']['{{ type }}'] -->
            {%- endif -%}
          {%- else %}
            {%- assign data = site.data[parent][part][type] -%}
            {%- if site.developer -%}
              <!-- documents.html(7): data = site.data['{{ parent }}']['{{ part }}']['{{ type }}'] -->
            {%- endif -%}
          {%- endif %}
        {%- endif -%}
      {%- endif -%}
      {%- unless data -%}{%- break -%}{%- endunless %}
      {%- assign docs = data[group] -%}
      {%- if site.developer and docs -%}
        <!-- documents.html(8): docs=data['{{ group }}'] exists -->
      {%- endif -%}
      {%- unless docs -%}
        {%- assign docs = data['@instances'][''][group] -%}
      {%- endunless -%}
    {%- endif -%}
    {%- if include.target == nil or cur_path == include.target %}
      {%- if docs -%}
        {%- if docs_exist == false and title != "None" -%}
          {%- if include.target == nil -%}
  <h3 id="{{ group_id }}">{%- if group_link -%}<a href="{{ group_link }}">{{ title }}</a>{%- else -%}{{ title }}{%- endif -%}</h3>
          {%- else -%}
  <h4 id="{{ group_id }}">{%- if group_link -%}<a href="{{ group_link }}">{{ title }}</a>{%- else -%}{{ title }}{%- endif -%}</h4>
          {%- endif -%}
        {%- endif -%}
        {%- assign doc_embed = "" -%}
        {%- assign docs_exist = true -%}
        {%- include gallery/begin.html style=doc_style -%}
        {%- for doc in docs -%}
          {%- assign doc_title = doc['@title'] -%}
          {%- assign doc_tags = doc['@tags'] | default: "" -%}
          {%- if doc_title and doc_title != "" and doc_tags contains doc_tag -%}
            {%- assign doc_file = doc['@file'] -%}
            {%- capture doc_path -%}{{ cur_path }}/{{ doc_file }}{%- endcapture -%}
            {%- assign doc_id = doc_file | default: doc_title | slugify -%}
            {%- assign doc_cover = doc['@cover'] -%}
            {%- unless doc_cover %}
              {%- if category == "software" and server != nil -%}
                {%- if doc_file contains ".jpg" or doc_file contains ".png" -%}
                  {%- comment -%}
                    Document cover and document path are the same...
                  {%- endcomment -%}
                  {%- capture doc_cover -%}https://{{ server }}.pcjs.org{{ cur_path | replace: "/software/", "/" }}/{{ doc_file }}{%- endcapture -%}
                  {%- capture doc_path -%}{{ doc_cover }}{%- endcapture -%}
                {%- endif -%}
              {%- endif -%}
            {%- endunless %}
            {%- unless doc_cover %}
              {%- assign doc_cover = "/documents/covers/default.jpg" -%}
            {%- endunless %}
            {%- assign doc_note = "" %}
            {%- assign doc_source = doc['@source'] -%}
            {%- if doc_source contains "PCjs" or doc_source contains "pcjs" -%}
              {%- assign doc_source = "" -%}
              {%- assign doc_note = "From the PCjs Archives" -%}
            {%- endif -%}
            {%- assign doc_link = doc['@link'] | default: doc_source | default: doc_path -%}
            {%- if doc_link == doc_source %}
              {%- assign doc_source = "" %}
            {%- endif %}
            {%- if doc_cover == "gif" or doc_cover == "jpg" or doc_cover == "jpeg" or doc_cover == "png" -%}
              {%- assign doc_cover = "." | append: doc_cover -%}
            {%- endif %}
            {%- assign doc_cover0 = doc_cover | slice: 0 %}
            {%- if doc_link contains "http" -%}{%- assign doc_external = true -%}{%- else -%}{%- assign doc_external = false -%}{%- endif -%}
            {%- comment -%}
              For now, we only embed the first document so marked, to keep things simple.
            {%- endcomment -%}
            {%- if doc['@embed'] and doc_embed == "" and include.target == nil -%}
              {%- assign doc_embed = doc['@embed'] -%}
              {%- assign embed_title = doc_title -%}
              {%- assign embed_link = doc_link -%}
              {%- assign embed_source = doc_source -%}
              {%- continue -%}
            {%- endif -%}
            {%- assign doc_width = doc['@width'] | default: cover_width -%}
            {%- assign doc_height = doc['@height'] | default: cover_height -%}
            {%- if doc_cover0 == "." -%}
              {%- capture doc_cover -%}{{ cur_path }}/covers/{{ doc_file | replace: ".pdf",doc_cover | replace: ".png",doc_cover }}{%- endcapture -%}
            {%- else %}
              {%- unless doc_cover contains "/" %}
                {%- capture doc_cover -%}{{ cur_path }}/covers/{{ doc_cover }}{%- endcapture -%}
              {%- endunless %}
            {%- endif -%}
            {%- include gallery/image.html id=doc_id image=doc_cover file=doc_file link=doc_link title=doc_title width=doc_width height=doc_height note=doc_note source=doc_source external=doc_external -%}
          {%- endif -%}
        {%- endfor -%}
        {%- include gallery/end.html -%}
        {%- if doc_embed != "" -%}
          {% include web/embed.html src=doc_embed title=embed_title link=embed_link source=embed_source %}
        {%- endif -%}
      {%- endif -%}
    {%- endif -%}
  {%- endif -%}
{%- endfor -%}
{%- if docs_exist == false %}
  <h3 id="{{ group_id }}">{{ title }}</h3>
  <p>No documents available for <strong>{{ path }}</strong></p>
{%- endif -%}
